import { Menu, shell } from 'electron'
import type { MenuItemConstructorOptions } from 'electron'
export const template: MenuItemConstructorOptions[] = [
  {
    label: '文件',
    submenu: [
      {
        label: '新建',
        accelerator: 'CmdOrCtrl+N',
        click: () => {
          console.log('新建')
        }
      },
      {
        label: '打开',
        accelerator: 'CmdOrCtrl+O',
        click: () => {
          console.log('打开')
        }
      }
    ]
  },
  {
    label: '编辑',
    submenu: [
      {
        label: '撤销',
        accelerator: 'CmdOrCtrl+Z',
        role: 'undo',
        click: () => {
          console.log('撤销')
        }
      },
      {
        label: '恢复',
        accelerator: 'CmdOrCtrl+Y',
        role: 'redo',
        click: () => {
          console.log('恢复')
        }
      },
      {
        type: 'separator'
      },
      {
        label: '剪切',
        accelerator: 'CmdOrCtrl+X',
        role: 'cut'
      },
      {
        label: '复制',
        accelerator: 'CmdOrCtrl+C',
        role: 'copy'
      },
      {
        label: '粘贴',
        accelerator: 'CmdOrCtrl+V',
        role: 'paste'
      },
      {
        label: '删除',
        role: 'delete'
      },
      {
        type: 'separator'
      },
      {
        label: '全选',
        role: 'selectAll'
      }
    ]
  },
  {
    label: '视图',
    submenu: [
      {
        label: '刷新',
        accelerator: 'CmdOrCtrl+R',
        role: 'reload'
      },
      {
        label: '强制刷新',
        accelerator: 'CmdOrCtrl+Shift+R',
        role: 'forceReload'
      },
      {
        label: '打开控制面板',
        accelerator: 'CmdOrCtrl+Shift+I',
        role: 'toggleDevTools'
      },
      {
        type: 'separator'
      },
      {
        label: '重置缩放',
        role: 'resetZoom'
      },
      {
        label: '缩小',
        role: 'zoomIn'
      },
      {
        label: '放大',
        role: 'zoomOut'
      },
      {
        type: 'separator'
      },
      {
        label: '全屏切换',
        role: 'togglefullscreen'
      }
    ]
  },
  {
    label: '窗口',
    submenu: [
      {
        label: '最小化',
        role: 'minimize',
        accelerator: 'CmdOrCtrl+M'
      },
      {
        label: '缩放',
        role: 'zoom'
      },
      {
        label: '关闭',
        role: 'close',
        accelerator: 'CmdOrCtrl+W'
      }
    ]
  },
  {
    label: '帮助',
    submenu: [
      {
        label: '文档',
        click: () => {
          shell.openExternal('https://www.electronjs.org/zh/docs/latest/')
        }
      },
      {
        label: '关于',
        role: 'about'
      }
    ]
  }
]

export const createMenu = (template: MenuItemConstructorOptions[]) => {
  const menu = Menu.buildFromTemplate(template)
  Menu.setApplicationMenu(menu)
}
